import random

data = []
f = open("sudoku.txt","r+")
for x in range(50):
    cur = []
    f.readline()
    for y in range(9):
        line = f.readline().rstrip('\n')
        cur.append([int(z) for z in line])
    data.append(cur)
#FILE STORED IN data VARIABLE

boxes = [(0,0),(3,0),(6,0),(0,3),(3,3),(6,3),(0,6),(3,6),(6,6)]
def getcurbox(x,y):
    while x % 3 != 0:
        x = x - 1
    while y % 3 != 0:
        y = y - 1
    return (x,y)

def getbox(d,t,r):
    current = []
    for x in range(3):
        for y in range(3):
            current.append(d[t+x][r+y])
    return current

def checkbox(d,t,r):
    current = getbox(d,t,r)
    for x in range(1,10):
        if current.count(x) > 1:
            return False
    return True

def posbox(d,t,r):
    current = getbox(d,t,r)
    return set(range(1,10))-set(current)
    
def checkrow(d,row):
    current = d[row]
    for x in range(1,10):
        if current.count(x) > 1:
            return False
    return True
    
def posrow(d,row):
    current = d[row]
    return set(range(1,10))-set(current)
    
def getcolumn(d,col):
    current = []
    for x in range(9):
        current.append(d[x][col])
    return current
    
def checkcolumn(d,col):
    current = getcolumn(d,col)
    for x in range(1,10):
        if current.count(x) > 1:
            return False
    return True

def poscol(d,col):
    current = getcolumn(d,col)
    return set(range(1,10))-set(current)

def getcands(current):
    cand = []
    for x in range(9):
        for y in range(9):
            if(current[x][y] == 0):
                b = getcurbox(x,y)
                cand.append(posbox(current,b[0],b[1]).intersection(posrow(current,x).intersection(poscol(current,y))))
            else:
                cand.append(set())
    return cand

def checkcands(cands):
    for cand in cands:
        if len(cand) > 0:
            return False
    return True
    

def solve(current):
    ri = -1
    rv = -1
    cands = getcands(current)
    while checkcands(cands) == False:
        test = 0
        for x in range(len(cands)):
            if len(cands[x]) == 1:
                test = 1
                for a in cands[x]:
                    current[x // 9][x % 9] = a
#                    cands[x].remove(a)
        if test == 0:
            #make guess
            if ri != -1:
                current[ri // 9][ri % 9] = 0
            r = random.randint(0,80)
            z = 0
            while True:
                if len(cands[z]) > 0:
                    r = r - 1
                    if r == 0:
                        rv = random.sample(cands[z],1)[0]
                        ri = z
                        current[ri // 9][ri % 9] = rv
#                        print ri
#                        print rv
                        break
                z = (z + 1) % 81
        cands = getcands(current)
    return current

    
answer = 0
num = 10
print(num+1)
current = data[num]
current = solve(current)
    
answer = answer + current[0][0] + current[0][1] + current[0][2]

print(answer)
#print(current)
        

'''
forx test in range(1,10):
    t = []
    for x in range(9):
        for y in range(9):
            if current[x][y] == 0:
                current[x][y] = test
                #NOW TEST IT
                for box in boxes:
                    if checkbox(current,box[0],box[1]) == False:
                        current[x][y] = 0
                for row in range(9):
                    if checkrow(current,row) == False:
                        current[x][y] = 0
                for col in range(9):
                    if checkcolumn(current,col) == False:
                        current[x][y] = 0
                '''
